package com.example.neutonote2.data.repository;

import android.app.Application;
import androidx.lifecycle.LiveData;
import com.example.neutonote2.data.dao.CategoryDao;
import com.example.neutonote2.data.db.NeuToNoteDatabase;
import com.example.neutonote2.data.model.Category;

import java.util.List;

/**
 * 分类仓库类，用于处理分类数据操作
 */
public class CategoryRepository {
    private final CategoryDao categoryDao;
    private final LiveData<List<Category>> allCategories;
    private final LiveData<List<Category>> defaultCategories;

    public CategoryRepository(Application application) {
        NeuToNoteDatabase db = NeuToNoteDatabase.getDatabase(application);
        categoryDao = db.categoryDao();
        allCategories = categoryDao.getAllCategories();
        defaultCategories = categoryDao.getDefaultCategories();
    }

    // 获取所有分类
    public LiveData<List<Category>> getAllCategories() {
        return allCategories;
    }
    
    // 获取默认分类
    public LiveData<List<Category>> getDefaultCategories() {
        return defaultCategories;
    }

    // 按ID获取分类
    public LiveData<Category> getCategoryById(int id) {
        return categoryDao.getCategoryById(id);
    }
    
    // 按名称获取分类
    public LiveData<Category> getCategoryByName(String name) {
        return categoryDao.getCategoryByName(name);
    }

    // 插入分类
    public void insert(Category category) {
        NeuToNoteDatabase.databaseWriteExecutor.execute(() -> {
            categoryDao.insert(category);
        });
    }

    // 更新分类
    public void update(Category category) {
        NeuToNoteDatabase.databaseWriteExecutor.execute(() -> {
            categoryDao.update(category);
        });
    }

    // 删除分类
    public void delete(Category category) {
        NeuToNoteDatabase.databaseWriteExecutor.execute(() -> {
            categoryDao.delete(category);
        });
    }
} 